<?php

namespace Home\Controller;

class SayController extends CommonController {

    public function index() {
        $count = M("say")->where("pid =0")->count();    //计算总数
        $Page = new \Think\Page($count, 10);
        $say = M("say")->limit($Page->firstRow . ',' . $Page->listRows)->where("pid =0")->order('id DESC')->select();
        foreach ($say as $k => $v) {
             $say[$k]['avatar'] = getSingleField($v['uid'], 'user', 'avatar');
            $say[$k]['huifu'] = M("say")->where("pid = " . $v['id'] . "")->order('addtime DESC')->select();
            foreach ($say[$k]['huifu'] as $k2 => $v2) {
                $say[$k]['huifu'][$k2]['avatar']= getSingleField($v2['uid'], 'user', 'avatar');
            }
        }
        $page = $Page->show();
        $this->assign("page", $page);
        if (session("userid")) {
            $user = M("user")->field("avatar,name")->where("id = " . session("userid") . "")->find();
            $this->assign("user", $user);
        }
        $this->assign("ctitle", "说 说");
        $this->assign("say", $say);
        $this->display("Index:say");
    }

    public function saysub() {
        header("Content-Type: text/html; charset=utf-8");
        $Dao = M('say');
        $data['addtime'] = time();
        $data['uid'] = Session::get(C('USER_UID'));
        $data['content'] = html_entity_decode($_POST['say']);
        $id = $Dao->add($data);
        if ($id > 0) {
            $this->redirect('Say/index');
            // echo "<script>alert('发布成功！');document.location.href='" . C('SITE_URL') . "index.php/say';</script>";
        } else {
            echo "<script>alert('发布失败！');history.go(-1);</script>";
        }
    }

}

?>
